package com.backmanage.controller;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSON;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.backmanage.entity.A;
import com.backmanage.entity.ReturnJSON;
import com.backmanage.entity.User;
import com.backmanage.intercept.AuthorityIntercept;
import com.backmanage.service.serviceI.BaseServiceI;
import com.backmanage.service.serviceI.UserServiceI;
import com.backmanage.service.serviceImpl.UserService;
import com.backmanage.utils.MD5Util;
import com.backmanage.utils.ParameterUtil;


@Controller
@RequestMapping("/user")
public class UserController {
		
		private static Logger log = Logger.getLogger ( UserController.class.getName ());
	
		@Autowired
		private UserServiceI userService;
		
		@Autowired
		private BaseServiceI baseService;

		
		@RequestMapping(value="/adduser",method=RequestMethod.GET)
		public ReturnJSON addUser(User user){
			
			ReturnJSON rj = new ReturnJSON();
			
			JSONObject json = new JSONObject();
			
			user.setPassword(MD5Util.toMD5(user.getPassword()));
			
			userService.saveOrUpdate(user);
			
			return rj;
		}
		/**
		 * 查询账号是否存在
		 * @param account
		 * @return
		 */
		@ResponseBody
		@RequestMapping(value="/isAccount",method=RequestMethod.GET)
		public ReturnJSON addUser(String account){
			
			ReturnJSON rj = new ReturnJSON();
			
			JSONObject json = new JSONObject();
			
			Map<String, Object> map = new HashMap<String, Object>();
			
			map.put("account", "admin1");
			
			List<User> list = baseService.getByProperty(User.class, map);
			
			json.put("account", list.size()>0);
			
			rj.setData(json);
			
			return rj;
		}
}
